package com.ts.manager;

import com.easemob.EMCallBack;
import com.easemob.chat.EMChatManager;
import com.easemob.chat.core.e;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.rio.core.L;
import com.rio.core.U;
import com.rio.helper.Sleeper;
import com.rio.helper.json.G;
import com.rio.layout.LayoutManager;
import com.rio.layout.TaskManager;
import com.rio.layout.view.SimpleTask;
import com.ts.api.BrightnessApi;
import com.ts.api.DownloadApi;
import com.ts.api.PingApi;
import com.ts.data.APIExecuteIMAction;
import com.ts.data.Instruct;
import com.ts.host.APP;
import com.ts.host.Event;
import com.ts.host.T;
import com.ts.host.TToast;

import de.greenrobot.event.EventBus;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Process;
import android.provider.Settings;
import android.provider.Settings.SettingNotFoundException;
import android.view.WindowManager;

public class PingManager {

	private static final int WAITING_TIME = 60 * 1000;//
	
	private static SimpleTask mTask;
	
	private static SimpleTask getTask(){
		if(U.isNull(mTask)){
			mTask = new SimpleTask() {
	
				@Override
				public Object onBGThread(Object... params) throws Exception {
					Sleeper.sleep(WAITING_TIME);
					getActon();
					return super.onBGThread(params);
				}
				
				
				@Override
				public void onUIThread(Object data, Object... params)
						throws Exception {
					postDelay();
					super.onUIThread(data, params);
				}
			};
		}
		return mTask;
		
	}


	public static void postDelay() {
			TaskManager.getInstance().async(getTask());
	}

	public static void getActon() {
		L.i("ping");
		HttpUtils http = new HttpUtils();
		http.configCurrentHttpCacheExpiry(0).configHttpCacheSize(0);
		http.send(HttpRequest.HttpMethod.GET, "https://www.baidu.com/",		
				new RequestCallBack<String>() {

					@Override
					public void onSuccess(ResponseInfo<String> responseInfo) {
						//连接外网
						if(WaterManager.getInstance().getMode() == WaterManager.MODE_FREE){
							L.i("kill");
							//MachineManager.getInstance().killProcess();
							//HttpMananger.regMachine();
							Intent i = new Intent(APP.ACTION);
							i.putExtra(APP.ACTION, Process.myPid());
							LayoutManager.getInstance().getActivity().startService(i);
							LayoutManager.getInstance().finish();
						}
					}

					@Override
					public void onFailure(HttpException error, String msg) {
						//不能连接服务器
						if(WaterManager.getInstance().getMode() == WaterManager.MODE_CHARGING){	
							EMChatManager.getInstance().logout(new EMCallBack() {
								
								@Override
								public void onSuccess() {
									// TODO Auto-generated method stub
									EventBus.getDefault().post(new Event.ConnectionChange(false));
								}
								
								@Override
								public void onProgress(int arg0, String arg1) {
									// TODO Auto-generated method stub
									
								}
								
								@Override
								public void onError(int arg0, String arg1) {
									// TODO Auto-generated method stub
									
								}
							});
							
						}
					}
				});

	}
	



	public static void setScreenBrightness(int brightness){
		Activity a = LayoutManager.getInstance().getActivity();
		if(U.notNull(a)){
	        ContentResolver resolver = a.getContentResolver();
			if(U.notNull(resolver) && PingManager.isAutoBrightness(resolver)){
				PingManager.stopAutoBrightness(resolver);
			}
			setBrightness(a,brightness);
		}
	}
	
	/**
	 * 判断是否开启了自动亮度调节
	 * 
	 * @param aContext
	 * @return
	 */
	private static boolean isAutoBrightness(ContentResolver aContentResolver) {
		boolean automicBrightness = false;
		try {
			automicBrightness = Settings.System.getInt(aContentResolver,
					Settings.System.SCREEN_BRIGHTNESS_MODE) == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC;
		} catch (SettingNotFoundException e) {
			e.printStackTrace();
		}
		return automicBrightness;
	}

	/**
	 * 获取屏幕的亮度
	 * 
	 * @param activity
	 * @return
	 */
	private static int getScreenBrightness(ContentResolver aContentResolver) {
		int nowBrightnessValue = 0;
		try {
			nowBrightnessValue = android.provider.Settings.System.getInt(
					aContentResolver, Settings.System.SCREEN_BRIGHTNESS);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return nowBrightnessValue;
	}

	/**
	 * 设置亮度
	 * 
	 * @param activity
	 * @param brightness
	 */
	private static void setBrightness(Activity activity, int brightness) {
		// Settings.System.putInt(activity.getContentResolver(),
		// Settings.System.SCREEN_BRIGHTNESS_MODE,
		// Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
		WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
		lp.screenBrightness = Float.valueOf(brightness) * (1f / 255f);
		activity.getWindow().setAttributes(lp);

	}

	/**
	 * 停止自动亮度调节
	 * 
	 * @param activity
	 */
	public static void stopAutoBrightness(ContentResolver aContentResolver) {
		Settings.System.putInt(aContentResolver,
				Settings.System.SCREEN_BRIGHTNESS_MODE,
				Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
	}

	/**
	 * 开启亮度自动调节
	 * 
	 * @param activity
	 */
	public static void startAutoBrightness(ContentResolver aContentResolver) {
		Settings.System.putInt(aContentResolver,
				Settings.System.SCREEN_BRIGHTNESS_MODE,
				Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
	}

	/**
	 * 保存亮度设置状态
	 * 
	 * @param resolver
	 * @param brightness
	 */
	private static void saveBrightness(ContentResolver resolver, int brightness) {
		Uri uri = android.provider.Settings.System
				.getUriFor("screen_brightness");
		android.provider.Settings.System.putInt(resolver, "screen_brightness",
				brightness);
		// resolver.registerContentObserver(uri, true, myContentObserver);
		resolver.notifyChange(uri, null);
	}
}
